<template>
  <el-dialog :title="titleMap[mode]" v-model="visible" :width="550" destroy-on-close @closed="$emit('closed')">
    <el-form :model="form" :rules="rules" ref="dialogForm" label-width="100px">
      <el-form-item label="ID" prop="id">
        <el-input v-model="form.id" placeholder="请输入ID" :readonly="mode=='show'" clearable></el-input>
      </el-form-item>
      <el-form-item label="所属任务ID" prop="taskId">
        <el-input v-model="form.taskId" placeholder="请输入所属任务ID" :readonly="mode=='show'" clearable></el-input>
      </el-form-item>
      <el-form-item label="任务名称" prop="name">
        <el-input v-model="form.name" placeholder="请输入任务名称" :readonly="mode=='show'" clearable></el-input>
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-input v-model="form.createTime" placeholder="请输入创建时间" :readonly="mode=='show'" clearable></el-input>
      </el-form-item>
      <el-form-item label="执行器" prop="actuator">
        <el-input v-model="form.actuator" placeholder="请输入执行器" :readonly="mode=='show'" clearable></el-input>
		<div v-if="mode=='add'">执行器: CLASS: | EXEC: | BEAN: | GET:http... | POST:http... </div>
      </el-form-item>
      <el-form-item label="触发规则" prop="schedule">
        <el-input v-model="form.schedule" placeholder="请输入触发规则" :readonly="mode=='show'" clearable></el-input>
		<div v-if="mode=='add'">触发规则: CRON: | INTERVAL: | MANUAL</div>
      </el-form-item>
      <el-form-item label="执行状态" prop="runStatus">
        <el-input v-model="form.runStatus" placeholder="请输入执行状态" :readonly="mode=='show'" clearable></el-input>
      </el-form-item>
      <el-form-item label="运行节点" prop="runHostname">
        <el-input v-model="form.runHostname" placeholder="请输入运行节点" :readonly="mode=='show'" clearable></el-input>
      </el-form-item>
      <el-form-item label="运行环境" prop="runEnvName">
        <el-input v-model="form.runEnvName" placeholder="请输入运行环境" :readonly="mode=='show'" clearable></el-input>
      </el-form-item>
      <el-form-item label="参数列表" prop="param">
        <el-input v-model="form.param" placeholder="请输入参数列表" clearable type="textarea" :readonly="mode=='show'" :autosize="{ minRows:2, maxRows:6 }"></el-input>
      </el-form-item>
      <el-form-item label="运行描述" prop="runRemarks">
        <el-input v-model="form.runRemarks" placeholder="请输入运行描述" clearable type="textarea" :readonly="mode=='show'" :autosize="{ minRows:2, maxRows:6 }"></el-input>
      </el-form-item>
      <el-form-item label="开始时间" prop="runTimeStart">
        <el-input v-model="form.runTimeStart" placeholder="请输入开始时间" :readonly="mode=='show'" clearable></el-input>
      </el-form-item>
      <el-form-item label="结束时间" prop="runTimeEnd">
        <el-input v-model="form.runTimeEnd" placeholder="请输入结束时间" :readonly="mode=='show'" clearable></el-input>
      </el-form-item>
    </el-form>
	<template #footer v-if="mode!='show'">
      <el-button @click="visible=false" >取 消</el-button>
      <el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
    </template>
  </el-dialog>
</template>

<script>
    import setting from './setting'
	export default {
		emits: ['success', 'closed'],
		data() {
			return {
				mode: "add",
				titleMap: {
					add: '新增',
					edit: '编辑',
					show: '查看'
				},
				visible: false,
				isSaveing: false,
				//表单数据
				form: {
					id: null,
					taskId: null,
					name: '',
					createTime: null,
					actuator: '',
					schedule: '',
					param: '',
					runStatus: '',
					runHostname: '',
					runEnvName: '',
					runRemarks: '',
					runTimeStart: null,
					runTimeEnd: null,
					_: ""
				},
				//验证规则
				/*
				rules: {
					sort: [
						{required: true, message: '请输入排序', trigger: 'change'}
					],
					name: [
						{required: true, message: '请输入名称'}
					]
				},
				*/
			}
		},
		mounted() {
		},
		methods: {
			//显示
			open(mode='add'){
				this.mode = mode;
				this.visible = true;
				return this
			},
			//表单提交方法
			submit(){
				this.$refs.dialogForm.validate(async (valid) => {
					if (valid) {
						this.isSaveing = true;
						var res;
						if (this.mode == "edit"){
							res = await setting.api.edit.post(this.form)
						} else {
							res = await setting.api.add.post(this.form)
						}
						this.isSaveing = false;
						if(res.code == setting.successCode){
							this.$emit('success', this.form, this.mode)
							this.visible = false;
							this.$message.success("操作成功")
						}else{
							this.$alert(res.message, "提示", {type: 'error'})
						}
					}
				})
			},
			//表单注入数据
			setData(data){
				this.form.id = data.id
				this.form.taskId = data.taskId
				this.form.name = data.name
				this.form.createTime = data.createTime
				this.form.actuator = data.actuator
				this.form.schedule = data.schedule
				this.form.param = data.param
				this.form.runStatus = data.runStatus
				this.form.runHostname = data.runHostname
				this.form.runEnvName = data.runEnvName
				this.form.runRemarks = data.runRemarks
				this.form.runTimeStart = data.runTimeStart
				this.form.runTimeEnd = data.runTimeEnd

				//可以和上面一样单个注入，也可以像下面一样直接合并进去
				//Object.assign(this.form, data)
			}
		}
	}
</script>

<style>
</style>
